package com.ftqh.core.jpa.service;

import com.ftqh.core.jpa.repository.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional
public class BaseServiceImpl<T, ID extends Serializable, R extends BaseRepository<T, ID>>implements BaseService<T, ID> {

	@Autowired
	protected R repository;


	@Override
	public T save(T entity) {
		return repository.save(entity);
	}

	@Override
	public Iterable<T> save(Iterable<T> entities) {
		return repository.save(entities);
	}

	@Override
	public T findOne(ID id) {
		return repository.findOne(id);
	}

	@Override
	public List<T> findAll() {
		return repository.findAll();
	}

	@Override
	public List<T> findAll(Iterable<ID> ids) {
		return repository.findAll(ids);
	}

	@Override
	public Page<T> findByAuto(T example, Pageable pageable) {
		return repository.findByAuto(example, pageable);
	}

	@Override
	public Page<T> findByAuto(T example, Integer page, Integer pageSize,Sort sort){
		Pageable pageable = new PageRequest(page, pageSize,sort);
		return this.findByAuto(example, pageable);
	}

	@Override
	public Page<T> findByAuto(T example, Integer page, Integer pageSize, Direction direction, String ... fieldNames){
		Sort sort = new Sort(direction, fieldNames);
		return this.findByAuto(example, page, pageSize, sort);
	}

	@Override
	public boolean exists(ID id) {
		return repository.exists(id);
	}

	@Override
	public void delete(ID id) {
		repository.delete(id);
	}

	@Override
	public void delete(T entity) {
		repository.delete(entity);
	}

	@Override
	public void deleteInBatch(Iterable<T> entities) {
		repository.deleteInBatch(entities);
	}

	@Override
	public void deleteAllInBatch() {
		repository.deleteAllInBatch();
	}

}